<?php

class Model_Equipment extends Zend_Db_Table_Abstract
{
    protected $_name = 'equipment';
    public function __construct()
    {
        $parameters =array(
                        'host'     => '127.0.0.1',
                        'username'   => 'pro_cmdb_server',
                        'password' => '0aFswAW7P2N8yVMdADXt',
                        'dbname'   => 'pro_cmdb_server',
                        'charset'  => "UTF8",
                        'adapter'  => 'Pdo_Mysql'
                   );
         try {
             $db = Zend_Db::factory('Pdo_Mysql', $parameters);
             $db->getConnection();
         } catch (Zend_Db_Adapter_Exception $e) {
    
         } catch (Zend_Exception $e) { 
         }
         //Zend_Db_Table::setDefaultAdapter($db);
         $this->_setAdapter($db);
    }
    // Thiết bị hết hạn - chuyển qua giai đoạn thuê chỗ
    public function getNotifyData(){
        $dataNotify = mktime(0, 0, 0, date("m")+1, date("d"), date("Y"));
        $dataNotify = date("Y-m-d", $dataNotify);
        
        $query = $this->select()      
                        ->setIntegrityCheck(false)
                        ->from(array('e' => $this->_name),array('e.equipmentId','e.equipmentName','e.code'))
                        ->joinLeft(array('u'=>'users'), 'e.managerId = u.userId' , array('username'))
                        ->joinLeft(array('c'=>'category'), 'c.categoryId = e.categoryId' , array('categoryName'))
                        ->joinInner(array('eq'=>'equipment_project'), 'eq.equipmentId = e.equipmentId', array())
                        ->joinInner(array('i'=>'items'), 'e.equipmentId = i.equipmentId', array())
                        ->where('i.duration <= ?', $dataNotify)
                        ->where('i.duration > i.startDate')
                        ->where('i.duration > 0')
                        ->group('e.equipmentId')
                        ->where('i.expire != ?',1);
          
        return $this->fetchAll($query)->toArray();
    }
    // Theo dõi thiết bị
    public function getTrackData(){
        $dataNotify = date("Y-m-d");
        
        $query = $this->select()      
                        ->setIntegrityCheck(false)
                        ->from(array('e' => $this->_name),array('e.equipmentId','e.equipmentName','e.code','e.trackDate','e.trackContent'))
                        ->joinLeft(array('u'=>'users'), 'e.managerId = u.userId' , array('username'))
                        ->joinLeft(array('c'=>'category'), 'c.categoryId = e.categoryId' , array('categoryName'))
                        ->joinInner(array('eq'=>'equipment_project'), 'eq.equipmentId = e.equipmentId', array())
                        ->joinInner(array('i'=>'items'), 'e.equipmentId = i.equipmentId', array())
                        ->where('e.trackDate > ?', 0)
                        ->where('e.trackDate <= ?', $dataNotify)
                        ->group('e.equipmentId');
        return $this->fetchAll($query)->toArray();
    }
}

